<template>
  <div>
    <el-button plain size="small" @click="showDialog">新增</el-button>

    <el-dialog title="新增" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="活动名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth" prop="region">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData">确 定</el-button>
      </div>
    </el-dialog>
  </div>

</template>


<script>
export default {
  name: "DialogView",
  data() {
    return {
      rules: {
        name: [
          {required: true, message: '请输入活动名称', trigger: 'blur'},
          {min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur'}
        ],
        region: [
          {required: true, message: '请输入活动区域', trigger: 'blur'}
        ]
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        name: '',
        region: ''
      }
    }
  },
  methods: {
    clearInfo() {
      console.log('dialog对话框已关闭')
      // 重置校验提示
      this.$refs.dialogForm.clearValidate()
      // 清空表单数据
      this.form = {
        name: '',
        region: ''
      }
    },
    submitData() {
      // 主动校验表单数据是否合法
      this.$refs.dialogForm.validate((val) => {
        if (val) {
          console.log('表单数据是:', JSON.stringify(this.form))
          // 发送http请求
        } else {
          console.log('请填写符合要求的表单数据')
        }
      })

    },
    showDialog() {
      this.dialogFormVisible = true
    }
  }
}
</script>


<style scoped>

</style>